package com.bawei.module_publish.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.lib_image.GlideApp
import com.bawei.module_publish.databinding.ItemToolsBinding
import com.bawei.module_publish.model.ToolsModelItem

/**
 *@author：lxy
 *@decription:
 *@time：2025/6/27
 */
class ToolsListAdapter(
    private val list: List<ToolsModelItem>,
    private val onItemClick: ((position: Int) -> Unit)? = null
) : RecyclerView.Adapter<ToolsListAdapter.ToolsViewHolder>() {

    inner class ToolsViewHolder(val binding: ItemToolsBinding) : ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToolsViewHolder {
        val binding = ItemToolsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ToolsViewHolder(binding)
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ToolsViewHolder, position: Int) {
        val item = list[position]
        val binding = holder.binding
        binding.tvTools.text = item.subTitle
        GlideApp.with(holder.itemView.context).load(item.subImage).into(binding.ivTools)
        // 设置点击事件
        binding.root.setOnClickListener {
            onItemClick?.invoke(position)
        }
    }
}